Skip to content

Conversation

@zharinov
Copy link
Member

@zharinov zharinov commented Jan 5, 2026

Summary

  • Trailing anchor constraints (. )) now trigger backtracking when the first match isn't the last child
  • Uses skip-retry pattern (same as quantifiers) to retry with next sibling until finding one that IS last
  • Adds 4 engine tests covering trailing anchor scenarios

Why

Queries like (program (function_declaration) @last .) failed to find the last function when multiple existed. The compiled bytecode had no checkpoint for retrying when UpSkipTrivia failed after a search match succeeded.

Notes

The fix adds compile_named_node_with_trailing_anchor() helper that generates:

entry: Match(nav, node_type) → down_wildcard
down_wildcard: Match(Down, wildcard) → try
try: epsilon → [body, retry_nav]          ← checkpoint
body: items (StayExact) → up_check
up_check: Match(UpSkipTrivia) → exit
retry_nav: Match(Next, wildcard) → try    ← advance on failure

@zharinov zharinov merged commit 9665f6c into master Jan 5, 2026
4 checks passed
@zharinov zharinov deleted the fix/trailing-anchor-backtracking branch January 5, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants